package org.jordens.jdbcspy.sync;

import org.jordens.jdbcspy.jmx.*;

import java.util.*;

/**
 * A JDBCSpyRecord represents a time-slice of jdbc activity.
 *
 * @author Adam Jordens
 */
public class JDBCSpyRecord
{
    private final List<QueryStatistic> queryStatistics;
    private final long totalSQLExecutionTime;
    private final long totalQueryExecutionCount;

    /**
     * @param queryStatistics QueryStatistics (that were updated during a particular time-slice)
     * @param totalSQLExecutionTime Total time spent executing SQL (during a particular time-slice)
     * @param totalQueryExecutionCount Total # of queries executed (during a particular time-slice)
     */
    public JDBCSpyRecord(List<QueryStatistic> queryStatistics,
                         long totalSQLExecutionTime,
                         long totalQueryExecutionCount)
    {
        this.totalQueryExecutionCount = totalQueryExecutionCount;
        this.totalSQLExecutionTime = totalSQLExecutionTime;
        this.queryStatistics = queryStatistics;
    }

    /**
     * Getter for property 'queryStatistics'.
     *
     * @return Value for property 'queryStatistics'.
     */
    public List<QueryStatistic> getQueryStatistics()
    {
        return queryStatistics;
    }

    /**
     * Getter for property 'totalSQLExecutionTime'.
     *
     * @return Value for property 'totalSQLExecutionTime'.
     */
    public long getTotalSQLExecutionTime()
    {
        return totalSQLExecutionTime;
    }

    /**
     * Getter for property 'totalQueryExecutionCount'.
     *
     * @return Value for property 'totalQueryExecutionCount'.
     */
    public long getTotalQueryExecutionCount()
    {
        return totalQueryExecutionCount;
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o == null || getClass() != o.getClass())
        {
            return false;
        }

        JDBCSpyRecord that = (JDBCSpyRecord) o;

        if (totalQueryExecutionCount != that.totalQueryExecutionCount)
        {
            return false;
        }
        if (totalSQLExecutionTime != that.totalSQLExecutionTime)
        {
            return false;
        }
        if (queryStatistics != null ? !queryStatistics.equals(that.queryStatistics) : that.queryStatistics != null)
        {
            return false;
        }

        return true;
    }

    /**
     * {@inheritDoc}
     */
    public int hashCode()
    {
        int result;
        result = (queryStatistics != null ? queryStatistics.hashCode() : 0);
        result = 31 * result + (int) (totalSQLExecutionTime ^ (totalSQLExecutionTime >>> 32));
        result = 31 * result + (int) (totalQueryExecutionCount ^ (totalQueryExecutionCount >>> 32));
        return result;
    }
}
